package com.manong.service.impl;

import com.alibaba.fastjson.JSON;
import com.manong.constant.RedisConstant;
import com.manong.domain.Employee;
import com.manong.exception.BusinessExp;
import com.manong.mapper.PermissionMapper;
import com.manong.service.IEmployeeService;
import com.manong.service.ILoginService;
import com.manong.util.RedisUtils;
import com.manong.util.VerifyCodeUtil;
import com.manong.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Map<String, String> code() {
        // 工具类帮我们生成验证码
        Map<String, String> map = VerifyCodeUtil.generateVerifyCode();
        // 把数据保存到redis当中
        // 数据类型：String类型  key：前缀：uuid value： code
        String uuid = map.get("uuid");
        String code = map.get("code");
        System.out.println(code);
//        stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_VERIFY_CODE + uuid, code);
        redisUtils.set(RedisConstant.LOGIN_VERIFY_CODE + uuid, code, RedisConstant.VERIFY_CODE_EXPIRE_TIME);
        // 返回map
        map.remove("code");
        return map;
    }

    @Override
    public Employee login(LoginVo loginVo) {
        // 1、判断vo不为空
        if(loginVo == null) {
            throw new BusinessExp("非法操作");
        }
        // 2、判断账号密码不能为空
        if(StringUtils.isEmpty(loginVo.getUsername()) || StringUtils.isEmpty(loginVo.getPassword())) {
            throw new BusinessExp("账号或者密码无能为空");
        }
        // 3、判断验证码不能为空
        if(StringUtils.isEmpty(loginVo.getCode())) {
            throw new BusinessExp("验证码不能为空");
        }
        // 4、上redis中查询验证码是否一致
        String redisCode = redisUtils.get(RedisConstant.LOGIN_VERIFY_CODE + loginVo.getUuid());
        if(StringUtils.isEmpty(redisCode)) {
            throw new BusinessExp("验证码错误");
        }
        // 5、对比传递过来的验证码是否一致
        Boolean flag = VerifyCodeUtil.verification(loginVo.getCode(), redisCode, true);
        if(!flag) {
            throw new BusinessExp("验证码错误");
        }
        // 6、根据账号和密码上数据库中去查询数据
        Employee employee = employeeService.login(loginVo.getUsername(), loginVo.getPassword());
        if(employee == null) {
            throw new BusinessExp("账号或者密码错误");
        }
        // 7、如果不为空，把用户信息保存到redis中
        redisUtils.set(RedisConstant.LOGIN_USER_INFO + employee.getId(), JSON.toJSONString(employee), RedisConstant.LOGIN_USER_EXPIRE_TIME);

        // 8、把当前用户拥有的权限表达式结合放到redis中
        // 根据用户id 查询用户拥有的权限表达式集合
        List<String> list = permissionMapper.selectExpressionsByEmpId(employee.getId());
        redisUtils.set(RedisConstant.LOGIN_INFO_EXPRESSIONS + employee.getId(), JSON.toJSONString(list), RedisConstant.LOGIN_USER_EXPIRE_TIME);

        return employee;
    }

    @Override
    public void logout(String userId) {
        // 删除redis中的用户信息
        redisUtils.delete(RedisConstant.LOGIN_USER_INFO + userId);
        // 删除redis中权限表达式信息
        redisUtils.delete(RedisConstant.LOGIN_INFO_EXPRESSIONS + userId);
    }
}
